package com.beauty.dressing.module.main.activity;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

import com.beauty.dressing.common.base.BaseFragmentActivity;
import com.beauty.dressing.common.bean.Video;
import com.beauty.dressing.common.video.IActivityVideo;
import com.beauty.dressing.common.video.IScreenOrientation;
import com.beauty.dressing.common.video.VideoPlayer;
import com.framework.common.utils.ILog;

/**
 * Created by Bond on 2016/12/08 10:25
 * you can contact me at qbaowei@qq.com
 */


public abstract class BaseVideoPlayerActivity extends BaseFragmentActivity implements IActivityVideo, VideoPlayer.ActivityListener, VideoPlayer.VideoViewHolderListener {

    protected Video mVideo;
    protected VideoPlayer mVideoPlayer;
    protected ImageButton mBackBtn;

    private IScreenOrientation.ScreenOrientationImpl mScreenOrientation;

    @Override
    protected void onCreate(Bundle bundle) {
        mScreenOrientation = new IScreenOrientation.ScreenOrientationImpl(this);
        super.onCreate(bundle);
        mScreenOrientation.initScreen();
    }

    @Override
    public void setContentView() {

    }

    @Override
    public void findView() {

    }

    @Override
    public void initData() {
        getRootView().setOnTouchListener(mVideoPlayer.getVideoViewTouchClick());
        if (mScreenOrientation.isLandscape()) {
            ILog.lw("[vp]landscape when init");
            mScreenOrientation.onScreenLandscape();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mVideoPlayer.onResume();
    }


    @Override
    protected void onPause() {
        super.onPause();
        mVideoPlayer.onPause();
    }

    @Override
    public void onClick(View v) {

    }

    /**
     * @return 返回根view
     */
    protected abstract View getRootView();

    @Override
    public Video getActivityVideo() {
        return mVideo;
    }

    @Override
    public void setActivityVideo(Video video) {
        //do nothing
    }

    @Override
    public void onClickFullScreen(VideoPlayer videoPlayer) {
        mScreenOrientation.clickFullScreen();
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public IScreenOrientation.ScreenOrientationImpl getScreenOrientationImpl() {
        return mScreenOrientation;
    }

    @Override
    public void onClickVideoBack() {
        onBackPressed();
    }

    @Override
    public void clearVideoPlayPositionCache() {
        VideoPlayer.VideoPositionCacheManager.getInstance().clearPosition();
    }

    @Override
    public View getFollowedView() {
        return null;
    }

    @Override
    public Video getVideo() {
        return mVideo;
    }

    @Override
    public int getFollowdViewAdapterPosition() {
        return 0;
    }

    @Override
    public void onClickReplayInVideoPlayer(Video video) {

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        ILog.ld("[vp]in");
        switch (newConfig.orientation) {
            case Configuration.ORIENTATION_PORTRAIT:
                mScreenOrientation.onScreenPortrait();
                break;
            case Configuration.ORIENTATION_LANDSCAPE:
                mScreenOrientation.onScreenLandscape();
                break;
            default:
                break;
        }
    }

    @Override
    public void onBackPressed() {
        int orientation = getResources().getConfiguration().orientation;
        switch (orientation) {
            case Configuration.ORIENTATION_PORTRAIT:
                super.onBackPressed();
                break;
            case Configuration.ORIENTATION_LANDSCAPE:
                mScreenOrientation.clickFullScreen();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        mVideoPlayer.onDestroy();
        VideoPlayer.VideoPositionCacheManager.getInstance().clearPosition();
        super.onDestroy();
    }
}
